﻿namespace TimeMachine.Engine.Metrics.Impl
{
	[MetricDescriptor("OCP")]
	public class OCPMetric : BaseMetric
	{
		private readonly FilesAddedMetric addedMetric;
		private readonly FilesDeletedMetric deletedMetric;
		private readonly FilesModifiedMetric modifiedMetric;

		public OCPMetric(
			FilesAddedMetric addedMetric,
			FilesDeletedMetric deletedMetric,
			FilesModifiedMetric modifiedMetric)
		{
			this.addedMetric = addedMetric;
			this.deletedMetric = deletedMetric;
			this.modifiedMetric = modifiedMetric;
		}

		public override MetricValue Calculate(int rev)
		{
			double ocp = (addedMetric.Calculate(rev).Value + deletedMetric.Calculate(rev).Value);
			if (ocp == 0) return new MetricValue(rev, Desc.MetricId, 0);
			return new MetricValue(rev, Desc.MetricId, ocp/(ocp + modifiedMetric.Calculate(rev).Value));
		}

		public override IAggregator CreateAggregator()
		{
			return new Average();
		}
	}
}